/*
 * Created on Aug 14, 2008
 * (c) 2009 by Reach Unlimited Corporation
 */
package com.mgmaps.maps;

import com.mgmaps.app.APPSettings;
import com.nutiteq.maps.GeoMap;
import com.nutiteq.maps.UnstreamedMap;
import com.nutiteq.maps.projections.EPSG3785;
import com.nutiteq.ui.StringCopyright;

public class MicrosoftMap extends EPSG3785 implements GeoMap, UnstreamedMap {

  private int mt;
  
  public static final int MT_ROAD = 0;
  public static final int MT_SATELLITE = 1;
  public static final int MT_HYBRID = 2;
  public static final int MT_TERRAIN = 3;
  public static final int MT_TRAFFIC = 4;
  
  private static final char[] LETTER = { 'r', 'a', 'h', 'r' };
  private static final String VERSION = "471";
  private static final String LANGUAGE = "en-us";

  public MicrosoftMap(final int mt) {
    super(new StringCopyright("Map data © Microsoft"), 256, 1, 17);
    if ((mt & MT_TRAFFIC) != 0)
      addTileOverlay(MicrosoftTrafficOverlay.TRAFFIC);
    this.mt = mt & ~MT_TRAFFIC;
    setMissingTileImage(APPSettings.missingTileImage);
  }

  public String buildPath(final int x, final int y, final int zoom) {
    final int tmpX = x >> 8;
    final int tmpY = y >> 8;
    final StringBuffer buf = new StringBuffer();
    buf.append("http://ecn.t");
    buf.append(((tmpY & 1) << 1) + (tmpX & 1));
    buf.append(".tiles.virtualearth.net/tiles/");
    buf.append(LETTER[mt]);
    for (int i = zoom - 1; i >= 0; i--)
      buf.append((((tmpY >> i) & 1) << 1) + ((tmpX >> i) & 1));
    buf.append(mt == 0 ? ".png" : ".jpeg");
    buf.append("?g=" + VERSION);
    buf.append("&mkt=" + LANGUAGE);
    buf.append("&n=z");
    if (mt == MT_TERRAIN)
      buf.append("&shading=hill");
    return buf.toString();
  }
}
